<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>阶段复习 | SongKaBaKa</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="">
    <meta name="description" content="我也不知道会不会有更深层次的理解，我打算再将部分数据类型、函数、类、文件操作等再复习一遍。那就让我们从字符串开始吧！ String常用方法：  title() 软  replace(‘\n,””)就是使用空来取代\n  upper() 软  lower() 软  lstrip() 软  rsttip() 软  strip() 软 不会删除中间的  capitalize() 软 串的首字母大写  c">
<meta property="og:type" content="article">
<meta property="og:title" content="阶段复习">
<meta property="og:url" content="https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/index.html">
<meta property="og:site_name" content="SongKaBaKa">
<meta property="og:description" content="我也不知道会不会有更深层次的理解，我打算再将部分数据类型、函数、类、文件操作等再复习一遍。那就让我们从字符串开始吧！ String常用方法：  title() 软  replace(‘\n,””)就是使用空来取代\n  upper() 软  lower() 软  lstrip() 软  rsttip() 软  strip() 软 不会删除中间的  capitalize() 软 串的首字母大写  c">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-09-22T14:26:47.178Z">
<meta property="article:modified_time" content="2020-08-14T09:47:43.815Z">
<meta property="article:author" content="Song Wani">
<meta name="twitter:card" content="summary">
    
    <link rel="shortcut icon" href="/blog/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Song Wani</h5>
          <a href="mailto:bugsong@wo.cn" title="bugsong@wo.cn" class="mail">bugsong@wo.cn</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://songbug.gitee.io/blog" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">阶段复习</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="Search">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">阶段复习</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-09-22T14:26:47.178Z" itemprop="datePublished" class="page-time">
  2020-09-22
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#String"><span class="post-toc-number">1.</span> <span class="post-toc-text">String</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#重构"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">重构</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#处理异常"><span class="post-toc-number">2.</span> <span class="post-toc-text">处理异常</span></a></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#测试代码"><span class="post-toc-number">3.</span> <span class="post-toc-text">测试代码</span></a></li></ol>
        </nav>
    </aside>


<article id="post-阶段复习"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">阶段复习</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-09-22 22:26:47" datetime="2020-09-22T14:26:47.178Z"  itemprop="datePublished">2020-09-22</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>我也不知道会不会有更深层次的理解，我打算再将部分数据类型、函数、类、文件操作等再复习一遍。那就让我们从字符串开始吧！</p>
<h1 id="String"><a href="#String" class="headerlink" title="String"></a>String</h1><p>常用方法：</p>
<ol>
<li><p>title() 软</p>
</li>
<li><p>replace(‘\n,””)就是使用空来取代\n</p>
</li>
<li><p>upper() 软</p>
</li>
<li><p>lower() 软</p>
</li>
<li><p>lstrip() 软</p>
</li>
<li><p>rsttip() 软</p>
</li>
<li><p>strip() 软 不会删除中间的</p>
</li>
<li><p>capitalize() 软 串的首字母大写</p>
</li>
<li><p>count() 统计出现次数</p>
</li>
<li><p>encode()</p>
</li>
<li><p>decode()</p>
</li>
<li><p>find() index() 还有带r-的，返回索引，错误的话，前者返回-1，后者报错</p>
</li>
<li><p>还有些is系列的不展开了，可以阅读Python自带文档</p>
</li>
<li><p>join(sequence) sequence,n,要连接的元素序列</p>
<ol>
<li><pre><code class="python">s1 = <span class="string">"-"</span>
s2 = <span class="string">""</span>
seq = (<span class="string">"r"</span>, <span class="string">"u"</span>, <span class="string">"n"</span>, <span class="string">"o"</span>, <span class="string">"o"</span>, <span class="string">"b"</span>) <span class="comment"># 字符串序列</span>
<span class="keyword">print</span> (s1.join( seq ))
<span class="keyword">print</span> (s2.join( seq ))
<span class="comment"># r-u-n-o-o-b</span>
<span class="comment"># runoob</span>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">15. ljust() 还有r-，左右对齐，第二个参数是填充字 </span><br><span class="line"></span><br><span class="line">菜鸟教程补充的：</span><br><span class="line"></span><br><span class="line">常用字符串运算符：</span><br><span class="line"></span><br><span class="line">1. +</span><br><span class="line"></span><br><span class="line">2. *</span><br><span class="line"></span><br><span class="line">3. [] 通过索引获取字符串中字符</span><br><span class="line"></span><br><span class="line">4. [:] 字符串切片，遵循**左闭右开**原则</span><br><span class="line"></span><br><span class="line">5. 成员运算符：</span><br><span class="line"></span><br><span class="line">   1. in 返回布尔值</span><br><span class="line">   2. not in 返回布尔值</span><br><span class="line"></span><br><span class="line">6. r 字符串引号前面加这个可以原始输出转义序列</span><br><span class="line"></span><br><span class="line">7. % 格式字符串</span><br><span class="line"></span><br><span class="line">   |      | 符号 | 描述                           |</span><br><span class="line">   | ---- | :--- | :----------------------------- |</span><br><span class="line">   |      | %c   | 格式化字符及其ASCII码          |</span><br><span class="line">   |      | %s   | 格式化字符串                   |</span><br><span class="line">   |      | %d   | 格式化整数                     |</span><br><span class="line">   |      | %u   | 格式化无符号整型               |</span><br><span class="line">   |      | %o   | 格式化无符号八进制数           |</span><br><span class="line">   |      | %x   | 格式化无符号十六进制数         |</span><br><span class="line">   |      | %X   | 格式化无符号十六进制数（大写） |</span><br><span class="line"></span><br><span class="line"># 列表</span><br><span class="line"></span><br><span class="line">基础概念不在赘述，主要讲一些常用的方法&amp;函数</span><br><span class="line"></span><br><span class="line">列表可以相加，可以重复：</span><br><span class="line"></span><br><span class="line">[1,2,3]+[4,5,6]  &gt;&gt;&gt; [1,2,3,4,5,6]</span><br><span class="line"></span><br><span class="line">[&#39;1&#39;]*3 &gt;&gt;&gt; [&#39;1&#39;,&#39;1&#39;,&#39;1&#39;] 注意星号位置</span><br><span class="line"></span><br><span class="line">## 切片：</span><br><span class="line"></span><br><span class="line">切片是一种浅拷贝</span><br><span class="line"></span><br><span class="line">### 浅拷贝：</span><br><span class="line"></span><br><span class="line">可以结合千峰笔记的案例来理解</span><br><span class="line"></span><br><span class="line">浅拷贝，使用copy() 方法，获得原有列表相同元素，但指向不同的空间</span><br><span class="line"></span><br><span class="line">使用内置模块 import copy</span><br><span class="line"></span><br><span class="line">num1 &#x3D; num.copy()  # 浅拷贝，内容相同，对象不同</span><br><span class="line"></span><br><span class="line">valuename &#x3D; copy.copy(object)  # 同上</span><br><span class="line"></span><br><span class="line">**这个要区别于将一个列表变量赋值给另一个列表变量**</span><br><span class="line"></span><br><span class="line">浅拷贝的话，内存不同，对象独立，如果含有嵌套的话，**&lt;u&gt;一维拷贝元素，多维拷贝指向&lt;&#x2F;u&gt;**。</span><br><span class="line"></span><br><span class="line">### 深拷贝：</span><br><span class="line"></span><br><span class="line">只能使用copy模块的deepcopy() 方法实现，**&lt;u&gt;全维度拷贝，类似于克隆&lt;&#x2F;u&gt;**</span><br><span class="line"></span><br><span class="line">words1 &#x3D; copy.deepcopy(words)</span><br><span class="line"></span><br><span class="line">## 修改元素：</span><br><span class="line"></span><br><span class="line">1. 使用索引修改</span><br><span class="line"></span><br><span class="line">## 添加元素：</span><br><span class="line"></span><br><span class="line">1. list.append(obj) </span><br><span class="line">2. list.extend(seq) 在列表一次性追加另一个序列的多个值</span><br><span class="line">3. list.insert(index,obj) 占据位置，原有后移</span><br><span class="line"></span><br><span class="line">## 删除元素：</span><br><span class="line"></span><br><span class="line">1. del 语句，好家伙，还能直接删除定义</span><br><span class="line">2. pop() 将元素从列表弹出，返回最后一个元素的值（参数可选）</span><br><span class="line">3. remove() 只删除遇到的第一个元素，可设置一个值为要删除元素的值的变量，实现删除后仍可以访问。不存在会报错</span><br><span class="line"></span><br><span class="line">## 查找与统计元素：</span><br><span class="line"></span><br><span class="line">1. list.count(obj)</span><br><span class="line">2. list.index(obj) 不存在的话报错，找到第一个就停止，返回索引</span><br><span class="line"></span><br><span class="line">## 函数：</span><br><span class="line"></span><br><span class="line">1. len() </span><br><span class="line">2. max()_</span><br><span class="line">3. min()</span><br><span class="line">4. list() 类型转换</span><br><span class="line"></span><br><span class="line">## 排序列表：</span><br><span class="line"></span><br><span class="line">1. list.sort() 字母升序，参数reverse可选，永久排序</span><br><span class="line">2. list.sorted() 字母升序，参数reverse可选，临时排序</span><br><span class="line">3. reverse() 永久性颠倒列表，不按照什么规则</span><br><span class="line"></span><br><span class="line">## 清空列表：</span><br><span class="line"></span><br><span class="line">list.clear() 清空列表</span><br><span class="line"></span><br><span class="line"># 字典</span><br><span class="line"></span><br><span class="line">## 方法：</span><br><span class="line"></span><br><span class="line">1. dict.clear() 删除字典内所有元素</span><br><span class="line">2. dict.copy() 返回一个字典的浅复制</span><br><span class="line">3. dict.fromkeys(seq[,value]) 创建一个新字典，以序列seq中元素做字典的键，val为字典所有键对应的初始值</span><br><span class="line">4. dict.get(key, default&#x3D;None) 返回指定键的值，不存在返回None</span><br><span class="line">5. key in dict 返回布尔值</span><br><span class="line">6. dict.items() 以列表返回可便利的（键，值）元组数据</span><br><span class="line">7. dict.keys() &lt;u&gt;返回一个（所有键）迭代器，可以使用list() 来转换为列表&lt;&#x2F;u&gt;</span><br><span class="line">8. dict.setdefault(key, default&#x3D;None) 和get类似，不存在则添加键key和值default</span><br><span class="line">9. dict.update(dict2) 把字典的键&#x2F;值对更新到dict里</span><br><span class="line">10. dict.values() 返回一个（所有值）迭代器，可以使用list来转换为列表</span><br><span class="line">11. pop(key[,default]) 删除给定的key值，返回被删除的key值。不给key值则返回default</span><br><span class="line">12. popitem() 随机返回并删除字典中最后一对键和值</span><br><span class="line"></span><br><span class="line"># 集合</span><br><span class="line"></span><br><span class="line">空集合是set() 一对大括号是空字典&#123;&#125;</span><br><span class="line"></span><br><span class="line">主要功能就是去重复；支持推导式建立；类似列表推导式</span><br><span class="line"></span><br><span class="line">## 添加元素：</span><br><span class="line"></span><br><span class="line">1. set.add(obj)</span><br><span class="line">2. set.update(obj)</span><br><span class="line"></span><br><span class="line">## 删除元素：</span><br><span class="line"></span><br><span class="line">1. set.remove(obj) 不存在则会报错</span><br><span class="line">2. set.discard(obj) 不存在没声音，不存在的话就不吱声</span><br><span class="line">3. set.pop() 集合无序，随机删除</span><br><span class="line"></span><br><span class="line">## emmm还有数学上的集合运算，建议阅读文档</span><br><span class="line"></span><br><span class="line"># 函数</span><br><span class="line"></span><br><span class="line">先讲参数，能够区分形参和实参，（初步猜测向函数传递参数的这一行为称为使用某参数调用函数）</span><br><span class="line"></span><br><span class="line">## 位置实参</span><br><span class="line"></span><br><span class="line">看起来像各个参数要对应位置，注意顺序，尽量符合使用习惯，（张三，男，18岁，未婚。。。）大概就是这样。</span><br><span class="line"></span><br><span class="line">## 关键字实参</span><br><span class="line"></span><br><span class="line">就是让人一看变量名就知道是用来干嘛的，能展示关键信息的字</span><br><span class="line"></span><br><span class="line">## 默认值</span><br><span class="line"></span><br><span class="line">像中间名，可以没有。我可以不用，但函数不能没有！</span><br><span class="line"></span><br><span class="line">## 等效的调用</span><br><span class="line"></span><br><span class="line">结合调用的概念。可以根据位置，直接写入要传递的实参；也可以使用向形参赋值的方式来传递实参。意思是可以使用不同的参数传递方式来调用函数。</span><br><span class="line"></span><br><span class="line">## 返回值</span><br><span class="line"></span><br><span class="line">使用return语句</span><br><span class="line"></span><br><span class="line">可以返回各种数据类型</span><br><span class="line"></span><br><span class="line">## 传递列表</span><br><span class="line"></span><br><span class="line">传递列表和字典等数据时，注意深拷贝和钱拷贝的区别，可以使用切片向函数传递副本。</span><br><span class="line"></span><br><span class="line">## 传递任意数量的实参</span><br><span class="line"></span><br><span class="line">使用 *形参名 作为参数，可以为接收到的实参立建立一个参数元组</span><br><span class="line"></span><br><span class="line">使用 **形参名 作为参数，可以为接收到的实参建立一个参数字典，传递时需要以 *名称&#x3D;‘值’* 的方式来传递</span><br><span class="line"></span><br><span class="line">## 导入模块的方式</span><br><span class="line"></span><br><span class="line">使用import可以导入整个模块，调用则需要使用句点表示法</span><br><span class="line"></span><br><span class="line">有from的话要先写from，凡是使用from导入的函数或者类，都可以直接使用，不必使用句点表示法</span><br><span class="line"></span><br><span class="line">form module_name import * 可以将模块内的所有东西导入进来，也不必使用句点表示法</span><br><span class="line"></span><br><span class="line">## as 语法</span><br><span class="line"></span><br><span class="line">1. 使用from module_name import function_name as new_name 来给导入的函数指定别名</span><br><span class="line">2. import module_name as new_name 来给导入的模块指定别名</span><br><span class="line"></span><br><span class="line"># 类</span><br><span class="line"></span><br><span class="line">## 建立一个类</span><br><span class="line"></span><br><span class="line">这里面有 __ init__（self不可少）这个特殊的方法，可以将属性进行初始化，而在之后的当前类名下的方法中，使用这些属性，用self来访问即可。方法可以有自己的形参，这都不互相干预</span><br><span class="line"></span><br><span class="line">## 根据类创建实例</span><br><span class="line"></span><br><span class="line">my_dog &#x3D; Dog(&#39;Dodo&#39;,3)</span><br><span class="line"></span><br><span class="line">向类传递必要的属性值，用来创建对应的实例。我的狗子叫豆豆，今年三岁。</span><br><span class="line"></span><br><span class="line">## 属性值</span><br><span class="line"></span><br><span class="line">这里也支持像函数那样的设置默认值，可以不用但不能没有。</span><br><span class="line"></span><br><span class="line">修改属性值可以通过句点表示法先访问到它，然后再进行修改。</span><br><span class="line"></span><br><span class="line">## 继承</span><br><span class="line"></span><br><span class="line">就是创建个子类，建立子类时，将父类的名字传递给子类。</span><br><span class="line"></span><br><span class="line">子类的方法__ init __（）初始化方法有些不同，需要传入父类的所有形参，包括self，然后再于方法内建立自己的  _ _ init _ _（）方法，使用super(). _ _init _ _(这里参数对应于父类除self之外的参数)，然后在同级缩进行写自己的初始化属性。</span><br><span class="line"></span><br><span class="line">## 重写父类方法</span><br><span class="line"></span><br><span class="line">使用同父类一样的方法名，即可实现重写（覆盖），Python会考虑子类的方法。</span><br><span class="line"></span><br><span class="line">## 将实例用作属性</span><br><span class="line"></span><br><span class="line">顾名思义，就是将另一个类（L）的初始化用作当前类（D）的属性。就是许多点的写法的原理。L有自己的属性（Ls）和方法（Lf），D也有自己的属性（Ds）和方法（Df），让L作为D的一个属性，即D_s1&#x3D;L(),将D实例化到ex_d &#x3D; D ,那么便可以通过D的实例来访问L的属性和方法：</span><br><span class="line"></span><br><span class="line">ex_d.D_s1.Ls</span><br><span class="line"></span><br><span class="line">ex_d.D_s1.Lf()</span><br><span class="line"></span><br><span class="line">写的粗糙，不理解的话，多看几遍。</span><br><span class="line"></span><br><span class="line">## 导入方法</span><br><span class="line"></span><br><span class="line">这个请参考函数。</span><br><span class="line"></span><br><span class="line">另外有模块依赖，像盖楼一样去理解，有了第一层才会有第二层，不存在空中楼阁。</span><br><span class="line"></span><br><span class="line">## Python标准库</span><br><span class="line"></span><br><span class="line">讲了一个字典键值对的有序获取方法。OrderedDict()</span><br><span class="line"></span><br><span class="line">## 类编码风格</span><br><span class="line"></span><br><span class="line">大驼峰命名法:WoDaiNiMenDa</span><br><span class="line"></span><br><span class="line">实例名用小写和下划线</span><br><span class="line"></span><br><span class="line">搭配文档字符串使用更佳。</span><br><span class="line"></span><br><span class="line"># 文件操作</span><br><span class="line"></span><br><span class="line">## 路径问题</span><br><span class="line"></span><br><span class="line">注意转移序列的使用，\是反斜杠，向上找就是俩点，建议使用相对路径</span><br><span class="line"></span><br><span class="line">新建文件夹使用得到了os模块，需要补充一下知识</span><br><span class="line"></span><br><span class="line">## 文件读取</span><br><span class="line"></span><br><span class="line">逐行读取，使用readlines()方法，会将文件的每一行作为一个元素建立列表，作为返回值。</span><br><span class="line"></span><br><span class="line">使用习惯就是</span><br><span class="line"></span><br><span class="line">- file_name&#x3D;&#39;路径&#39;</span><br><span class="line">- path&#x3D;&#39;路径&#39;</span><br><span class="line"></span><br><span class="line">读取方式的话，记住w写，r读，a附加，wb使用二进制读取。。</span><br><span class="line"></span><br><span class="line">with open(file_name,&#39;r&#39;,encoding&#x3D;&#39;utf-8&#39;) as file_object:</span><br><span class="line"></span><br><span class="line">​	lines&#x3D;file_object.readlines()</span><br><span class="line"></span><br><span class="line">......</span><br><span class="line"></span><br><span class="line">## 文件写入</span><br><span class="line"></span><br><span class="line">&#96;&#96;&#96;Python</span><br><span class="line">with open(text_file, &#39;a&#39;, encoding&#x3D;&#39;utf-8&#39;) as txt_obj:</span><br><span class="line">    txt_obj.write(one_sentence)</span><br><span class="line">#意思就是向text_file指向的本地文件追加写入one_setence的内容</span><br></pre></td></tr></table></figure>
<span class="comment">## 存储数据</span>
</code></pre>
</li>
</ol>
</li>
</ol>
<p>使用json模块</p>
<p>主要使用json.dump()与json.load() 方法</p>
<p>大概就是存储和加载的意思。</p>
<p>与文件读写的方式相同，看起来更像是换了一种形式，使用json.dump(代存储数据，目标文件)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line">numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">filename = <span class="string">'number_list.json'</span></span><br><span class="line"><span class="keyword">with</span> open(filename, <span class="string">'w'</span>) <span class="keyword">as</span> num_obj:</span><br><span class="line">    json.dump(numbers, num_obj)</span><br></pre></td></tr></table></figure>

<p>程序执行完毕后，会在同级目录下新建number_list.json文件，并存储列表numbers</p>
<p>json.dumps()用于将dict类型转换为str类型，因为直接写dict数据会报错</p>
<p>json.loads()用于将str类型转换为dict类型，这两个函数主要用于不涉及文件的操作点</p>
<p>读取与写入无太大差别。</p>
<h2 id="重构"><a href="#重构" class="headerlink" title="重构"></a>重构</h2><p>这个主要内容就是讲的优化精简代码，提高复用率。</p>
<h1 id="处理异常"><a href="#处理异常" class="headerlink" title="处理异常"></a>处理异常</h1><p>特地将这个作为大板块来讲，我觉得挺重要的</p>
<p>熟练使用try-except代码块</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    print(<span class="number">5</span>/<span class="number">0</span>)</span><br><span class="line"><span class="keyword">except</span> ZeroDivisionError:</span><br><span class="line">    <span class="comment">#except后指定发生的错误</span></span><br><span class="line">    print(<span class="string">"零不能做除数！"</span>)</span><br></pre></td></tr></table></figure>

<p>还有个带else的，逻辑是try通过了会执行这个else代码块。这个根据需求场景使用，若需要返回执行结果，则可以用try-except-else来设置代码块。</p>
<p>这种行为可以避免泄露代码，防止发生错误的回溯给敌人信息！</p>
<p>类似错误还有ImportError，FileNotFoundError……</p>
<h1 id="测试代码"><a href="#测试代码" class="headerlink" title="测试代码"></a>测试代码</h1><p>pass</p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2020-08-14T09:47:43.815Z" itemprop="dateUpdated">2020-08-14 17:47:43</time>
</span><br>


        
        这里可以写作者留言，标签和 hexo 中所有变量及辅助函数等均可调用，示例：<a href="/blog/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/" target="_blank" rel="external">https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/</a>
        
    </div>
    
    <footer>
        <a href="https://gitee.com/songbug/blog.git">
            <img src="/blog/img/avatar.jpg" alt="Song Wani">
            Song Wani
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            

            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/&title=《阶段复习》 — SongKaBaKa&pic=https://gitee.com/songbug/blog.git/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/&title=《阶段复习》 — SongKaBaKa&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《阶段复习》 — SongKaBaKa&url=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/&via=https://gitee.com/songbug/blog.git" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2020/09/23/CSS3%E7%9A%84%E5%AD%A6%E4%B9%A0/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">CSS3的学习</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2020/09/22/%E9%87%8D%E5%A4%8D%E5%BC%95%E7%94%A8%E9%97%AE%E9%A2%98/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">重复引用问题</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/blog/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/blog/img/wechat.jpg" data-alipay="/blog/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>This blog is licensed under a <a rel="license noopener" href="https://creativecommons.org/licenses/by/4.0/" target="_blank">Creative Commons Attribution 4.0 International License</a>.</span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Song Wani &copy; 2019 - 2021</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/&title=《阶段复习》 — SongKaBaKa&pic=https://gitee.com/songbug/blog.git/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/&title=《阶段复习》 — SongKaBaKa&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《阶段复习》 — SongKaBaKa&url=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/&via=https://gitee.com/songbug/blog.git" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://gitee.com/songbug/blog.git/2020/09/22/%E9%98%B6%E6%AE%B5%E5%A4%8D%E4%B9%A0/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
